package com.holly.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.holly.mapper.StudentMapper;
import com.holly.pojo.Clazz;
import com.holly.pojo.PageResultData;
import com.holly.pojo.Student;
import com.holly.pojo.StudentQueryParam;
import com.holly.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author holly
 * @create 2025-07-03 13:45
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageResultData<Student> page(StudentQueryParam param) {
        //1.设置分页参数
        PageHelper.startPage(param.getPage(), param.getPageSize());
        //2.执行查询
        List<Student> studentList = studentMapper.list(param);
        //3.解析查询结果，并封装
        Page<Student> p = (Page<Student>) studentList;
        return new PageResultData<Student>(p.getTotal(), p.getResult());
    }

    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        student.setViolationCount((short) 0);
        student.setViolationScore((short) 0);
        studentMapper.insert(student);
    }

    @Override
    public Student getById(Integer id) {
        return studentMapper.getById(id);
    }

    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.delete(ids);
    }

    @Override
    public void updateViolation(Integer id, Integer scoreNum) {
        studentMapper.updateViolation(id,scoreNum);
    }
}
